var prefix = ctx+ "/system/dictinoary";
$(document).ready(function ()
{
    //树加载
    nodes = [];
    get_nodes();
    layui.use(['tree', 'layer'], function ()
    {
        var layer = layui.layer
        , $ = layui.jquery;

        layui.tree({
            elem: '#demo1' //指定元素
          , target: '_blank' //是否新选项卡打开（比如节点返回href才有效）
          , click: function (item)
          {
              get_dictinoary_List(item.value);
              $("#span_currentType").html(" 【" + item.name + "：" + item.value + "】");
              $("#span_currentType").attr("dictinoaryType_Value", item.value);
              $("#span_currentType").attr("dictinoaryType_Name", item.name);
              //layer.msg('当前节名称：' + item.name + '<br>全部参数：' + JSON.stringify(item));
              //console.log(item);
          }
          , nodes: nodes
        });
    });

    layui.use('table', function ()
    {
        table = layui.table;
        table.render({
            elem: '#test'
          , url:ctx+  '/system/dictinoary/list'
          , cols: [[
           // { field: 'id', width: 80, title: 'ID', sort: true } ,
            { field: 'itemvalue', width: 150, title: '字典取值' }
            , { field: 'itemname', width: 250, title: '字典名称', sort: true }
            //, { field: 'dictinoaryType', width: 200, title: '字典类型编码' }
            , { field: 'description', width: 150, title: '描述' }
            , { field: 'right', title: '操作', width: 177, toolbar: "#barDemo" }
          ]]
          , page: true
            , where: {
                dictinoaryType: "null"
            }
        });

        //监听工具条
        table.on('tool(demo)', function (obj)
        {
            var data = obj.data;
            if (obj.event === 'del')
            {
                //obj.del();
                //layer.close(index);
                remove(data.id,data.dictinoaryType);
            } else if (obj.event === 'edit')
            {
                //layer.alert('编辑行：<br>' + JSON.stringify(data));
                
                edit(data.id,$("#span_currentType").attr("dictinoaryType_Name"));
            }
        });
    });


    $("#btn_add").on("click", function ()
    {
        add();
    });
});

function get_nodes()
{
    $.ajax({
        type: "Get",
        url:ctx+  "/system/dictinoaryType/tree",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false,
        error: function (request)
        {
            parent.layer.alert("Connection error");
        },
        success: function (data)
        {
            nodes = data;
        }
    });
}

//数据列表加载
function get_dictinoary_List(dictinoaryType)
{
    table.render({
        elem: '#test'
          , url: ctx+ '/system/dictinoary/list'
          , cols: [[
           // { field: 'id', width: 80, title: 'ID', sort: true } ,
            { field: 'itemvalue', width: 150, title: '字典取值' }
            , { field: 'itemname', width: 250, title: '字典名称', sort: true }
            //, { field: 'dictinoaryType', width: 200, title: '字典类型编码' }
            , { field: 'description', width: 150, title: '描述' }
			, { field: 'right', title: '操作', width: 177, toolbar: "#barDemo" }
          ]]
          //, page: true
            , where: {
                dictinoaryType: dictinoaryType
            }
    });
}

function add()
{
    var value = $("#span_currentType").attr("dictinoaryType_Value");
    var name = $("#span_currentType").attr("dictinoaryType_Name");

    if (value != undefined && name != undefined)
    {
        layer.open({
            type: 2,
            title: '增加',
            maxmin: true,
            shadeClose: false, // 点击遮罩关闭层
            area: ['800px', '520px'],
            content: prefix + '/add?dictinoaryName=' + name + '&dictinoaryValue=' + value
        });
    }
    else
    {
        layer.msg("请先选择左侧【字典类型】");
    }
}
function edit(id,dictinoaryTypeName)
{
    layer.open({
        type: 2,
        title: '编辑',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '520px'],
        content: prefix + '/edit?id=' + id+'&dictinoaryTypeName=' + dictinoaryTypeName
    });
}
function remove(id,dictinoaryType)
{
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消']
    }, function ()
    {
        $.ajax({
            url: prefix + "/remove",
            type: "post",
            data: {
                'id': id
            },
            success: function (r)
            {
                if (r.code == 0)
                {
                    layer.msg(r.msg);
                	get_dictinoary_List(dictinoaryType);
                } else
                {
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function reload()
{
    window.location.reload();
}
function batchRemove()
{
    var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
    if (rows.length == 0)
    {
        layer.msg("请选择要删除的数据");
        return;
    }
    layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
        btn: ['确定', '取消']
        // 按钮
    }, function ()
    {
        var ids = new Array();
        // 遍历所有选择的行数据，取每条数据对应的ID
        $.each(rows, function (i, row)
        {
            ids[i] = row['id'];
        });
        $.ajax({
            type: 'POST',
            data: {
                "ids": ids
            },
            url: prefix + '/batchRemove',
            success: function (r)
            {
                if (r.code == 0)
                {
                    layer.msg(r.msg);
                    reLoad();
                } else
                {
                    layer.msg(r.msg);
                }
            }
        });
    }, function ()
    {

    });
}